package cn.itcast.wanxinp2p.account.service.impl;

import cn.itcast.wanxinp2p.account.service.SmsService;
import cn.itcast.wanxinp2p.common.domain.RestResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {
     @Resource
    RedisTemplate<String,String> redisTemplate;
    @Value("${host}")
  private   String host;
    @Value("${path}")
 private    String path;
    @Value("${method}")
  private   String method;
    @Value("${appcode}")
  private   String appcode;
    @Override
    public RestResponse getSMSCode(String mobile) {
//            String host = "https://dfsns.market.alicloudapi.com";
//            String path = "/data/send_sms";
//            String method = "POST";
//            String appcode = "1ff9a2369f174e5b98aa26fca419ba9c";
      System.out.println(appcode+"kkkkk");
        Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            //根据API的要求，定义相对应的Content-Type
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, String> querys = new HashMap<String, String>();
            Map<String, String> bodys = new HashMap<String, String>();

            int code=(int) ((Math.random() * 9 + 1) * (int) Math.pow(10, 5));//6位验证码
        redisTemplate.opsForValue().set("code:"+mobile, String.valueOf(code),5, TimeUnit.MINUTES);
          System.out.println("code::::"+code);
            bodys.put("content", "code:"+code);
            bodys.put("phone_number", mobile);
            bodys.put("template_id", "TPL_0000");
            try {
            //    HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
             //   System.out.println(response.toString());
                //获取response的body
                //System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            Map<String,String> map=new HashMap<>();
            map.put("key","code:"+mobile);
            return RestResponse.success(map);
        }
    }

